unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, Buttons;

const
  W = 20;
  H = 20;
  N = 10;
  X = 50;
  Y = 100;
  L = 20;
  PW = 50;
  PH = 50;

type
  TForm1 = class(TForm)
    st: TBitBtn;
    timer: TTimer;
    SB: TScrollBar;
    Timer2: TTimer;
    SB1: TScrollBar;
    procedure FormCreate(Sender: TObject);
    procedure timerTimer(Sender: TObject);
    procedure SBChange(Sender: TObject);
    procedure stClick(Sender: TObject);
    procedure Timer2Timer(Sender: TObject);
    procedure SB1Change(Sender: TObject);
  private
    { Private declarations }
  public
    procedure Change;
    { Public declarations }
  end;

var
  Form1: TForm1;
  mass: array of Integer;
  step, rn, s: Integer;
  pan: array of TPanel;

implementation

{$R *.dfm}

{ TForm1 }

procedure TForm1.Change;
var i, j: Integer;
begin
  for i:= 0 to N-1 do
  begin
    if (i <> step) and (i <> step+1) then
    begin
      pan[i].Color:=clBtnFace;
      pan[i].Caption:=intToStr(mass[i]);
    end else begin
      pan[i].Color:=clRed;
      pan[i].Caption:=intToStr(mass[i]);
    end;
    if i >= N-rn then
    begin
      pan[i].Color:=clWhite;
    end;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  i: Integer;
begin
  SetLength (pan, N);
  for i:= 0 to N-1 do
  begin
    pan[i]:=TPanel.Create(Form1);
    pan[i].Width:=PW;
    pan[i].Height:=PH;
    pan[i].Left:= X + i*L + (i+1)*PW;
    pan[i].Top:= Y;
    pan[i].Parent:=Form1;
  end;
end;

procedure TForm1.timerTimer(Sender: TObject);
var a: Integer;
begin
  s:=0;
  if step = N-1-rn then
  begin
    step:=0;
    rn:=rn+1;
    if rn = N-1 then
    begin
      timer.Enabled:=false;
    end;
  end;
  if (mass[step] > mass[step+1]) then
  begin
    a:=mass[step];
    mass[step]:=mass[step+1];
    mass[step+1]:=a;
    if s = 0 then
    begin
      timer2.Enabled:= True;
      timer.Enabled:=False;
      exit;
    end;
    s:=0;
  end;
  Change;
  step:=step+1;
end;

procedure TForm1.SBChange(Sender: TObject);
begin
  timer.Interval:=SB.Position*10;
end;

procedure TForm1.stClick(Sender: TObject);
var i: Integer;
begin
  for i:= 0 to N-1 do
  begin
    pan[i].Left:= X + i*L + (i+1)*PW;
  end;
  step:=0;
  rn:=0;
  SetLength (mass, N);
  for i:= 0 to N-1 do
  begin
    mass[i]:=Random (50);
  end;
  timer.Enabled:=True;
  Change;
end;

procedure TForm1.Timer2Timer(Sender: TObject);
var
  a, b, i: Integer;
begin
  Change;
  if pan[step+1].Left > L*(step) + PW*(step+1) + X then
  begin
    pan[step].Left:=pan[step].Left + 5;
    pan[step+1].Left:=pan[step+1].Left - 5;
  end else begin
    a:=pan[step].Left;
    b:=StrToInt(pan[step].Caption);
    pan[step].Left:=pan[step+1].Left;
    pan[step+1].Left:=a;
    pan[step].Caption:=pan[step+1].Caption;
    pan[step+1].Caption:=IntToStr(b);
    s:=1;
    Timer2.Enabled:=False;
    timer.Enabled:=true;
  end;
end;

procedure TForm1.SB1Change(Sender: TObject);
begin
  Timer2.Interval:=SB1.Position;
end;

end.
